const app = getApp()
import {
  showMsg
} from '../../../../utils/config.js'

import {
  validateEmoji,
  validateSpecial,
  validateMobile,
  validateMobile2
} from '../../../../utils/validate.js'
let spacePopoverSel = require('../behaviors/space/space-popover-sel.js')
import {
  reqalarmReveiverAdd,
  reqOrgList
} from '../../../../request/api'
Page({
  behaviors: [spacePopoverSel], //空间弹窗选择混入
  data: {
    navHeight: app.globalData.navHeight + 'px', //头部距离
    bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
    rolename: '',
    form: {
      name: '',
      mobile: '',
      level: 1,
      orgName: "",
      orgId: "",
    },
    showUnitSelection: false, //是否展示单位选择弹窗
    receiveTypeList: [{
      id: 1,
      name: '公众号',
      active: false,
    }, {
      id: 2,
      name: '短信',
      active: false,
    }, {
      id: 0,
      name: '语音',
      active: false,
    }, ],
    disabledBtn: false,
    showlevel: false,
    levelName: "一级",
    levelList: [{
      id: 1,
      text: "一级"
    }, {
      id: 2,
      text: "二级"
    }, {
      id: 3,
      text: "三级"
    }, {
      id: 4,
      text: "四级"
    }],
    apporgId: "", //==1为全部单位
    showorg: false,
    orgList: [],
  },
  onLoad(e) {
    this.setData({
      rolename: app.globalData.rolename
    })
    this.getParkId(e)
  },
  onShow() {
    // this.setData({
    //     apporgId:app.globalData.orgId
    // })
    // this.getOrgList()
    // if(app.globalData.orgId==1) return;
    this.getSpaceList() //安装点列表
  },
  // 单位选择弹窗打开
  onUnitSelection() {
    this.setData({
      showUnitSelection: true
    })
  },
  confirmUnitSelection(e) {
    console.log('e111', e);
    const {
      showUnitSelection,
      orgInfo
    } = e.detail
    this.setData({
      showUnitSelection,
    })
    if (orgInfo && orgInfo.id) {
      this.setData({
        "form.orgId": orgInfo.id,
        "form.orgName": orgInfo.orgName,
        showorg: false,
        // 安装点
        spaceName: "",
        spaceId: ""
      })
      this.getClone() //清除楼层安装点选中状态
      this.getInitBuildList() //建筑物回到最初状态
    }
  },
  // 获取单位id
  getParkId(e) {
    let params = JSON.parse(e.params)
    console.log(params);
    this.setData({
      spaceId: params.spaceId,
      spaceName: params.spaceName,
    })
    if (params.orgId) {
      this.setData({
        "form.orgId": params.orgId,
        apporgId: ""
      })
    } else {
      this.setData({
        "form.orgId": '',
        apporgId: 1
      })
    }
  },
  // 获取名字
  nameChange(e) {
    this.setData({
      'form.name': e.detail.value
    })
  },
  // 清除名字
  nameClear() {
    this.setData({
      'form.name': ''
    })
  },
  // 获取手机号
  mobileChange(e) {
    this.setData({
      'form.mobile': e.detail.value
    })
  },
  // 清除手机号
  mobileClear() {
    this.setData({
      'form.mobile': ''
    })
  },

  // 获取通知方式
  receiveTypeChange(e) {
    let {
      id
    } = e.currentTarget.dataset
    let {
      receiveTypeList
    } = this.data
    receiveTypeList.forEach(item => {
      if (item.id === id) {
        if (e.detail) {
          item.active = true
        } else {
          item.active = false
        }
      }
    })
    this.setData({
      receiveTypeList
    })
  },
  // 确认新增
  confirm() {
    if (this.data.disabledBtn) return
    let {
      form,
      spaceId,
      receiveTypeList
    } = this.data
    if (!form.name) return showMsg("请输入真实姓名")
    if (!form.mobile) return showMsg("请输入手机号码")
    if (!form.orgId) return showMsg("请选择单位")
    if (!validateMobile2(form.mobile)) return
    let params = {
      // orgId: app.globalData.orgId ==1?form.orgId:app.globalData.orgId,
      orgId: form.orgId,
      spaceId,
      name: form.name,
      mobile: form.mobile,
      // mobileReceive: receiveTypeList[0].active ? 1 : 0,
      // mpReceive: receiveTypeList[1].active ? 1 : 0,
      // msgReceive: receiveTypeList[2].active ? 1 : 0,
      mpReceive: receiveTypeList[0].active ? 1 : 0,
      msgReceive: receiveTypeList[1].active ? 1 : 0,
      mobileReceive: receiveTypeList[2].active ? 1 : 0,
      level: spaceId ? form.level : ''
    }
    this.reqAddFun(params)
  },
  // 请求接口
  async reqAddFun(params) {
    const res = await reqalarmReveiverAdd(params)
    if (res.code != 20000) return showMsg(res.message);
    showMsg("新增成功", "success")
    this.setData({
      disabledBtn: true
    })
    setTimeout(() => {
      wx.navigateBack({
        delta: 1,
      })
    }, 2000)
  },
  // 弹出
  clicklevel() {
    this.setData({
      showlevel: true
    })
  },
  // 隐藏
  levelClose() {
    this.setData({
      showlevel: false
    })
  },
  // 确定监管等级
  levelConfirm(e) {
    console.log(e);
    this.setData({
      levelName: e.detail.value.text,
      "form.level": e.detail.value.id,
      showlevel: false,
    })
  },

  // 弹出单位
  showorgClick() {
    this.setData({
      showorg: true
    })
  },
  // 关闭单位
  orgClose() {
    this.setData({
      showorg: false
    })
  },
  // 单位确认
  orgConfirm(e) {
    console.log(e);
    this.setData({
      "form.orgId": e.detail.value.id,
      "form.orgName": e.detail.value.orgName,
      showorg: false,
      // 安装点
      spaceName: "",
      spaceId: ""
    })
    this.getClone() //清除楼层安装点选中状态
    this.getInitBuildList() //建筑物回到最初状态
  },
  // 获取组织列表
  async getOrgList() {
    let params = {
      tenantId: app.globalData.tenantId,
      userId: app.globalData.userId,
      roleName: app.globalData.rolename,
    }
    const res = await reqOrgList(1, 99, params)
    if (res.code !== 20000) return showMsg(res.message)
    let {
      rows
    } = res.data
    console.log(rows);
    this.setData({
      orgList: rows
    })
  },

})